<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll" >
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="todoTask"
      @keydown.enter="enterFn"
    />
  </header>
</template>

<script>
import { mapState,mapMutations } from  'vuex'
export default {
  data() {
    return {
      todoTask:'',
      todoTaskList:[]
    }
  },
  computed:{
   ...mapState(['list']),
   isAll : {
    set(val){
       this.list.forEach(item=>item.isDone = val)
    },
    get(){
      return this.list.length && this.list.every(item => item.isDone)
    }
   }
  },
  methods:{
    ...mapMutations(['addTodoTaskFn']),
    enterFn(){
       if(this.todoTask === '') {
        return alert('请输入信息')
       }
       this.addTodoTaskFn({
         id: this.list.length ? this.list.at(-1).id + 1 : 100 ,
         name: this.todoTask,
         isDone: false
       })
       // 清空输入框
       this.todoTask = ''
    }
  }
}
</script>
